﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PinLayoutMVC.Models
{
    public class QuestionModel
    {
        public int SurveyID { get; set; }
        public int ID { get; set; }
        public int Number { get; set; }
        public string Question { get; set; }
        public string Description { get; set; }
        public EQuestionType Type { get; set; }
        public Boolean AllowFeedback { get; set; }
        public string FeedbackText { get; set; }
        public List<AnswerModel> Answers { get; set; }
        public Boolean Validated { get; set; } //Hoeft niet in DB
        public string ValidationMessage { get; set; } //Hoeft niet in DB

        public enum EQuestionType
        {
            Dropdown,
            radiobutton,
            checkbox,
            open
        }

        public QuestionModel(){
             this.Answers = new List<AnswerModel>();
        }

        public void AddValidationMessage(string message)
        {
            if (!string.IsNullOrEmpty(ValidationMessage))
                ValidationMessage += " | ";

            ValidationMessage += message;
        }

        private void clearValidation()
        {
            Validated = false;
            ValidationMessage = string.Empty;
        }

        public void Validate()
        {
            clearValidation();

            if (string.IsNullOrEmpty(Question))
                AddValidationMessage("No question text");

            if (AllowFeedback && string.IsNullOrEmpty(FeedbackText))
                AddValidationMessage("No feedback text");

            if (Type == EQuestionType.open && Answers.Count != 1)
                AddValidationMessage("More then 1 answer not allowed at open questions");

            if (Type != EQuestionType.open && !(Answers.Count >= 2))
                AddValidationMessage("2 answers or more required");
            
            if(Answers.Where(a => !a.Validate()).Any())
                AddValidationMessage("1 or more invalid answers");

            Validated = true;
        }

        public static EQuestionType GetQuestionType(long? id)
        {
            EQuestionType QuestionType = new EQuestionType();
            switch (id)
            {

                  case 0:
                    QuestionType = EQuestionType.checkbox;
                    break;
                  case 1:
                    QuestionType = EQuestionType.Dropdown;
                    break;
                  case 2:
                    QuestionType = EQuestionType.open;
                    break;
                  case 3:
                    QuestionType = EQuestionType.radiobutton;
                    break;
                default:
                    QuestionType = EQuestionType.open;
                    break;
            }
            return QuestionType;
        }


    }
}